package com.timoon.sample.web.user.panel;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.Options;
import com.timoon.app.utils.GlobalRandom;
import com.timoon.component.behavior.my97datepicker.My97DatePickerTextField;
import com.timoon.component.behavior.validateengine.ValidateBehavior;
import com.timoon.component.panel.common.utils.ValidatorFeedbackPanel;
import com.timoon.component.panel.jqgrid.JqGridPanel;
import com.timoon.component.panel.jqgrid.action.SaveButton;
import com.timoon.component.panel.jqgrid.panel.CreatorPanel;
import com.timoon.component.panel.tree.treetable.ThreeColumnTabelPanel;
import com.timoon.sample.dao.UserRoleDAO;
import com.timoon.sample.dao.model.TUser;
import com.timoon.sample.dao.model.UserRole;
import com.timoon.sample.dao.model.UserRoleExample;
import com.timoon.sample.web.user.UserService;

public class UserCreatorPanel extends CreatorPanel {
	private static final long serialVersionUID = 5609151316916067846L;
	
	@SpringBean
	private UserService userService;
	
	@SpringBean
	private UserRoleDAO userRoleDAOImpl;

	//为表单生成随机id
	private String formClass="form-"+GlobalRandom.getRandom();
	
	private TUser user;
	
	
	public UserCreatorPanel(String id,JqGridPanel mainPanel,final String recordId) {
		super(id,mainPanel,recordId);
		// TODO Auto-generated constructor stub
		
		//初始化user
		if(recordId==null){
			user=new TUser();
		}else{
			user=(TUser) mainPanel.getService().loadRecordById(recordId);
		}
		
		//表单
		Form form=new Form("wk_form");
		form.add(new AttributeModifier("class",formClass));
		this.add(form);
		
		
		//启动验证插件
		this.add(new ValidateBehavior("."+formClass, new Options("promptPosition","'bottomRight'")));
		
		TextField<String> nameInput=new TextField<>("wk_name_input",new PropertyModel(user, "name"));
		nameInput.add(new AttributeModifier("class", "validate[required,minSize[2]]"));
		form.add(nameInput);
		
		TextField<String> accountInput=new TextField<>("wk_account_input",new PropertyModel(user, "account"));
		accountInput.add(new AttributeModifier("class", "validate[required,minSize[2]]"));
		form.add(accountInput);
		
		TextField<String> emailInput=new TextField<>("wk_email_input",new PropertyModel(user, "email"));
		emailInput.add(new AttributeModifier("class", "validate[required,custom[email]]"));
		form.add(emailInput);
		
		TextField<String> phoneInput=new TextField<>("wk_phone_input",new PropertyModel(user, "phone"));
		phoneInput.add(new AttributeModifier("class", "validate[required,custom[phone]]"));
		form.add(phoneInput);
		
		//角色列表
		Map<String,String> configMap=new HashMap<String,String>();
		configMap.put(ThreeColumnTabelPanel.COLUMN1_TITLE_CONFIGMAP_KEY, "角色名称");
		configMap.put(ThreeColumnTabelPanel.COLUMN1_EXPRESSION_CONFIGMAP_KEY, "text");
		configMap.put(ThreeColumnTabelPanel.COLUMN2_TITLE_CONFIGMAP_KEY, "备注");
		configMap.put(ThreeColumnTabelPanel.COLUMN2_EXPRESSION_CONFIGMAP_KEY, "remark");
		
		//获取当前用户已拥有的role.id集合
		List<String> roleIds=this.getRoleIdsByUser(recordId);
		final ThreeColumnTabelPanel treeTable=new ThreeColumnTabelPanel("wk_role",userService.getAllRole(),roleIds,configMap);
		form.add(treeTable);
		
		
		//出生日期
//		ChineseDatePicker bornDate=new ChineseDatePicker("wk_born",new Model<Date>(new Date()));
//		form.add(bornDate);
		
		My97DatePickerTextField<Date> born=new My97DatePickerTextField<>("wk_born",new Model<Date>(new Date()) );
		form.add(born);
		
		form.add(new SaveButton("wk_saveBtn",mainPanel){
			private static final long serialVersionUID = 1104981176828048324L;

			@Override
			protected Map<String, Object> getInputData() {
				// TODO Auto-generated method stub
				Map<String,Object> map=new HashMap<String, Object>();
				map.put("user", user);
				map.put("roleIds", treeTable.getSelectedIds());
				return map;
			}
			@Override
			protected ValidatorFeedbackPanel getFeedback() {
				// TODO Auto-generated method stub
				
				return feedback;
			}
		});
	}


	/**
	 * 通过用户id获取其所拥有的角色id集合
	 * @param userid
	 * @return
	 */
	private List<String> getRoleIdsByUser(final String userid){
		List<String> roleIds=new ArrayList<String>();
		if(userid!=null){
			@SuppressWarnings({"unchecked" })
			List<UserRole> userRolesOfCurrentUser=this.getUserRoleDAOImpl().selectByExample(new UserRoleExample(){
				{
					this.createCriteria().andUserIdEqualTo(Integer.valueOf(userid));
				}
			});
			if(userRolesOfCurrentUser!=null){
				for(UserRole userRole:userRolesOfCurrentUser){
					roleIds.add(userRole.getRoleId().toString());
				}
			}
		}
		return roleIds;
	}
	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public UserRoleDAO getUserRoleDAOImpl() {
		return userRoleDAOImpl;
	}


	public void setUserRoleDAOImpl(UserRoleDAO userRoleDAOImpl) {
		this.userRoleDAOImpl = userRoleDAOImpl;
	}
	
	
	

}
